package com.qingdai.ordercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.qingdai.business.emuns.Coupon;
import com.qingdai.business.emuns.Order;
import com.qingdai.business.emuns.Shop;
import com.qingdai.business.entities.CommonResult;
import com.qingdai.business.service.CouponService;
import com.qingdai.business.service.OrderService;
import com.qingdai.business.service.ShopService;
import com.qingdai.business.utils.JwtUtil;
import com.qingdai.business.utils.TimeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/coupon")
@Tag(name = "优惠卷")
public class CouponController {
    @Resource
    private CouponService couponService;
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private ShopService shopService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private TimeUtil timeUtil;


    //获取所有优惠卷
//    @GetMapping("/all/xyz")
//    @Operation(summary = "所有优惠卷", description = "获取所有优惠卷")
//    public CommonResult getAllCoupon() {
//        List<Coupon> allCoupon = couponService.list();
//        return new CommonResult(200, "获取所有优惠卷", allCoupon);
//    }

    //用户获取自己优惠卷
    @GetMapping("/jwt")
    @Operation(summary = "用户获取自己优惠卷", description = "用户获取自己优惠卷")
    public CommonResult getCouponByJwt(HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();
        LambdaQueryWrapper<Coupon> w = new LambdaQueryWrapper<>();
        w.like(Coupon::getUserList, jwtUserId);
        List<Coupon> allCoupons = couponService.list(w);
        if (allCoupons == null) {
            return new CommonResult(404, "未找到优惠卷");
        }
        return new CommonResult(200, "用户获取自己优惠卷", allCoupons);
    }

    //根据shopId获取优惠卷
    @GetMapping("/jwt/shopId")
    @Operation(summary = "根据shopId获取优惠卷", description = "根据shopId获取优惠卷")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult getCouponByShopId(@RequestParam(required = true) Integer shopId,
                                         HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();

        LambdaQueryWrapper<Coupon> w = new LambdaQueryWrapper<>();
        w.eq(Coupon::getShopId, shopId);
        List<Coupon> couponsList = couponService.list(w);
        if (couponsList == null) {
            return new CommonResult(404, "未找到优惠卷");
        }
        Coupon coupon = couponsList.get(0);
        Integer shopIdByCoupon = coupon.getShopId();
        Integer sellerId = shopService.getById(shopIdByCoupon).getSellerId();
        if (jwtUserId != sellerId) {
            return new CommonResult(400, "请勿查询他人店铺优惠卷");
        }
        return new CommonResult(200, "根据shopId获取优惠卷成功", couponsList);
    }

    //创建新优惠卷
    @PostMapping("/jwt")
    @Operation(summary = "创建新优惠卷", description = "创建新优惠卷")
    @Parameters({
            @Parameter(name = "shopId", description = "店铺id", required = true),
            @Parameter(name = "rule", description = "规则类型", required = true),
            @Parameter(name = "ruleDetail", description = "规则细节", required = true),
            @Parameter(name = "time", description = "有效期", required = true),
            @Parameter(name = "userList", description = "具此用户", required = true),
    })
    public CommonResult createCouponByJwt(@RequestParam(required = true) Integer shopId,
                                         @RequestParam(required = true) String rule,
                                         @RequestParam(required = true) String ruleDetail,
                                         @RequestParam(required = true) String time,
                                         @RequestParam(required = true) String userList,
                                         HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();
        Integer sellerId = shopService.getById(shopId).getSellerId();
        if (sellerId != jwtUserId) {
            return new CommonResult(400, "不具备创建该店铺优惠卷权限");
        }
        boolean Result = couponService.save(new Coupon(null, shopId, rule, ruleDetail, time, userList,1));
        if (!Result) {
            return new CommonResult(400, "创建优惠卷失败");
        }
        return new CommonResult(200, "创建优惠卷成功");
    }


    //    //根据id更新优惠卷
    @PutMapping("/jwt/id")
    @Operation(summary = "更新优惠卷", description = "根据id更新优惠卷")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "shopId", description = "店铺id", required = false),
            @Parameter(name = "rule", description = "规则类型", required = false),
            @Parameter(name = "ruleDetail", description = "规则细节", required = false),
            @Parameter(name = "time", description = "有效期", required = false),
            @Parameter(name = "userList", description = "具此用户", required = false),
    })
    public CommonResult updateCouponByIdAndJwt(@RequestParam Integer id,
                                     @RequestParam(required = false) Integer shopId,
                                     @RequestParam(required = false) String rule,
                                     @RequestParam(required = false) String ruleDetail,
                                     @RequestParam(required = false) String time,
                                     @RequestParam(required = false) String userList,
                                     HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();
        Integer sellerId = shopService.getById(shopId).getSellerId();
        if (sellerId != jwtUserId) {
            return new CommonResult(400, "不具备修改该店铺优惠卷权限");
        }
        Coupon coupon = couponService.getById(id);
        if (coupon == null) {
            return new CommonResult(400, "id非法");
        }
        Boolean checkResult = timeUtil.effectiveCheck(coupon.getTime());
        if (checkResult) {
            return new CommonResult(400, "该优惠卷已生效,无法编辑");
        }
        boolean Result = couponService.saveOrUpdate(new Coupon(id, shopId, rule, ruleDetail, time, userList,null));
        if (!Result) {
            return new CommonResult(400, "修改新优惠卷失败");
        }
        return new CommonResult(200, "修改优惠卷成功");
    }

    //根据id获取优惠卷
    @GetMapping("/jwt/id")
    @Operation(summary = "根据id获取优惠卷", description = "根据id获取优惠卷")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult getCouponByIdAndJwt(@RequestParam(required = true) Integer id,
                                     HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();
        Coupon coupon = couponService.getById(id);
        if (coupon == null) {
            return new CommonResult(404, "未找到优惠卷");
        }
        Shop shop = shopService.getById(coupon.getShopId());
        String userList = coupon.getUserList();
        if (shop.getSellerId() != jwtUserId && !userList.contains(jwtUserId.toString())) {
            return new CommonResult(404, "不具备查询该id优惠卷权限");
        }
        return new CommonResult(200, "根据id:" + id + ",获取优惠卷成功", coupon);


    }

    //根据id删除优惠卷
    @DeleteMapping("/jwt/id")
    @Operation(summary = "根据id删除优惠卷", description = "根据id删除优惠卷")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult deleteCouponById(@RequestParam(required = true) Integer id,
                                         HttpServletRequest request) {
        CommonResult result = jwtUtil.getUserIdByRequest(request);
        Integer jwtUserId = (Integer) result.getData();
        Coupon coupon = couponService.getById(id);
        if (coupon == null) {
            return new CommonResult(404, "根据id未找到优惠卷");
        }
        Shop shop = shopService.getById(coupon.getShopId());
        if (!Objects.equals(shop.getId(), jwtUserId)) {
            return new CommonResult(404, "请勿删除他人商店优惠卷");
        }
        String time = coupon.getTime();
        Boolean checkResult = timeUtil.effectiveCheck(time);
        if (checkResult) {
            return new CommonResult(400, "该优惠卷已生效,无法删除");
        }
        boolean deleteResult = couponService.removeById(id);
        if (!deleteResult) {
            return new CommonResult(400, "根据id删除优惠卷失败");
        }
        return new CommonResult(200, "根据id删除优惠卷成功");
    }
}




